<template>
  <div class="search-container">
    <el-form
      :inline="true"
      label-position="left"
      style="width: 100%"
      :model="formState"
    >
      <el-row style="width: 100%">
        <el-col :span="item.span" :key="item.field" v-for="item in props.form">
          <el-form-item
            :label="item.label"
            class="left-align-label"
            :prop="item.field"
          >
            <el-input
              v-if="item.component === 'Input'"
              v-model="formState[item.field]"
              v-bind="item.componentProps"
            >
            </el-input>
            <el-select
              v-if="item.component === 'Select'"
              v-model="formState[item.field]"
              v-bind="item.componentProps"
            >
              <el-option
                :label="jtem.label"
                :value="jtem.value"
                :key="jtem.value"
                v-for="jtem in item.componentProps.options"
              ></el-option>
            </el-select>
            <el-date-picker
              v-if="item.component === 'DatePicker'"
              v-model="formState[item.field]"
              v-bind="item.componentProps"
            />
            <el-checkbox-group
              v-if="item.component === 'CheckboxGroup'"
              v-model="formState[item.field]"
              v-bind="item.componentProps"
            >
              <el-checkbox
                :label="jtem.label"
                :value="jtem.value"
                :key="jtem.value"
                v-for="jtem in item.componentProps.options"
              />
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col span="6">
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button type="primary" @click="hanleReset">重置</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script setup>
import { defineProps, ref, defineExpose } from "vue";
const props = defineProps({
  form: {
    type: Array,
    required: true,
  },
  loadData: {
    type: Function,
    required: true,
  },
  reset: {
    type: Function,
  },
  search: {
    type: Function,
    required: true,
  },
});
const formState = ref({});

const handleSearch = () => {
  props.search(formState.value);
};

const hanleReset = () => {
  formState.value = {};
  props.reset(formState.value);
};

defineExpose({
  formState,
});
</script>
<style scoped>
:deep(.el-row) {
  margin-top: 20px;
}
:deep(.el-form-item) {
  width: 100% !important;
}
</style>
